namespace NovellSecurity
{
  using System.DirectoryServices.Protocols;
  using System;
  using System.Net;
  using System.Security.Cryptography.X509Certificates;
  using Sitecore.Diagnostics;

  public static class ConnectionHelper
  {
    public static LdapConnection Connect(string connectionStringName, string userDn, string password)
    {
      string hostName = System.Configuration.ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
      LdapConnection con = new LdapConnection(hostName);
      //con.SessionOptions.SecureSocketLayer = true;
      //con.SessionOptions.VerifyServerCertificate = new VerifyServerCertificateCallback(ServerCallback);
      con.Credential = new NetworkCredential(userDn, password);
      con.AuthType = AuthType.Basic;
      return con;
    }

    /// <summary>
    /// Checking the SSL certificate. Due to the server provides us with it, we do not need to check so deep.
    /// </summary>
    /// <param name="connection"></param>
    /// <param name="certificate"></param>
    /// <returns></returns>
    public static bool ServerCallback(LdapConnection connection, X509Certificate certificate)
    {
      return true;
    }
  }
}
